package org.create.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.create.bean.Constants;

import java.io.*;
import java.util.Arrays;
import java.util.Map;


public class DataTool {
    public static String strToHump(String str) {
        return strToHump(str, true, true, true);
    }

    public static String strToHump(String str, boolean prefix) {
        return strToHump(str, prefix, true, true);
    }

    public static String strToHump(String str, boolean prefix, boolean capitalize) {
        return strToHump(str, prefix, true, capitalize);
    }

    /**
     * @param str
     * @param prefix     是否保留前缀
     * @param suffix     是否保留后缀
     * @param capitalize 第一个词首字母是否大写
     * @return
     */
    public static String strToHump(String str, boolean prefix, boolean suffix, boolean capitalize) {
        StringBuilder sb = new StringBuilder();
        checkStr(str);
        if (!prefix) {
            str = str.substring(str.indexOf('_') + 1);
        }
        if (!suffix) {
            str = str.substring(0, str.lastIndexOf('_') == -1 ? str.length() : str.lastIndexOf('_'));
        }
        for (String word : str.split("_")) {
            if (word.isEmpty()) {
                continue;
            }
            if (!capitalize) {
                capitalize = true;
                sb.append(word);
                continue;
            }
            sb.append(word.substring(0, 1).toUpperCase()).append(word.substring(1));
        }
        return sb.toString();
    }

    public static void checkStr(String str) {
        if (str == null || str.isEmpty()) {
            throw new RuntimeException("字符串不能为空");
        }
    }

    public static boolean isExist(String[] strMap, String sqlType) {
        return Arrays.stream(strMap).anyMatch((e) -> e.equals(sqlType));
    }

    public static String sqlTypeToJavaType(String sqlType, Map<String, String> resultMap) {
        checkStr(sqlType);
        if (ArrayUtils.contains(Constants.SQL_INTEGER_TYPE, sqlType)) {
            return resultMap.put("javaType", "Integer");
        } else if (ArrayUtils.contains(Constants.SQL_LONG_TYPE, sqlType)) {
            return resultMap.put("javaType", "Long");
        } else if (ArrayUtils.contains(Constants.SQL_STRING_TYPE, sqlType)) {
            return resultMap.put("javaType", "String");
        } else if (ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES, sqlType)) {
            resultMap.put("dateTime", "true");
            return resultMap.put("javaType", "Date");
        } else if (ArrayUtils.contains(Constants.SQL_DATE_TYPES, sqlType)) {
            resultMap.put("date", "true");
            return resultMap.put("javaType", "Date");
        } else if (ArrayUtils.contains(Constants.SQL_DECIMAL_TYPE, sqlType)) {
            resultMap.put("bigDecimal", "true");
            return resultMap.put("javaType", "BigDecimal");
        } else {
            throw new RuntimeException("类型错误--->" + sqlType);
        }
    }

    public static void saveJsonToFile(String json) {
        saveJsonToFile(json, "./data-@.json");
    }

    public static void saveJsonToFile(String json, String path) {
        File file = new File(path);
        try {
            file = new File(path);
            boolean r = file.createNewFile();
            LogTool.printLog(r, "新建文件: ", file);
        } catch (IOException e) {
            LogTool.printError(e);
        }
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.append(json);
            fileWriter.close();
        } catch (Exception e) {
            LogTool.printError(e);
        }
    }

    public static String firstChatToUpper(String str){
        if (str == null || str.isEmpty()) {
            return "";
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String firstChatToLower(String str){
        if (str == null || str.isEmpty()) {
            return "";
        }
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static void main(String[] args) {
        System.out.println(strToHump("tb_user_id", false, false));
        System.out.println(Constants.PATH_BASE_JAVA);
        System.out.println(Constants.PATH_PO);
    }
}
